package edu.cmu.aes.project2;

import java.math.BigInteger;
import java.rmi.RemoteException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import edu.cmu.aes.project2.ws.data.UserInformationDto;
import edu.cmu.aes.project2.ws.data.UserInformationServiceProxy;

public class UserInformationService {

	private String digest(String password) throws NoSuchAlgorithmException{
		MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
		digest.update(password.getBytes(),0, password.length());
		return new BigInteger(1,digest.digest()).toString(16);		
	}
	
	
	public Boolean authentication(String userName, String password) {
		edu.cmu.aes.project2.ws.data.UserInformationService userInformationService = new UserInformationServiceProxy();
		Boolean authenticationResult = null;
		try {
			authenticationResult = userInformationService.authentication(userName, digest(password));
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return authenticationResult;
	}
	
	public UserInformationDto getUserInformation(String userName){
		edu.cmu.aes.project2.ws.data.UserInformationService userInformationService = new UserInformationServiceProxy();
		UserInformationDto userInfoDto = null;
		try {
			userInfoDto = userInformationService.getUserInformation(userName);
			return userInfoDto;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return userInfoDto;
	}
}
